package com.taysear.parcel;

import javax.persistence.EntityManager;

import com.taysear.parcel.domain.User;
import com.taysear.parcel.util.Constants;
import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.themes.Reindeer;

public class PasswordResetLayout extends Form{

	public PasswordResetLayout(final ParcelApplication app) {
		setCaption("  ");
		final PasswordField currentPassword = new PasswordField("Current password");
		currentPassword.setRequired(true);
		currentPassword.setRequiredError("Field is required");
		final PasswordField newPassword = new PasswordField("New password");
		newPassword.setRequired(true);
		newPassword.setRequiredError("Field is required");
		final PasswordField confirmNewPassword = new PasswordField("Confirm password");
		confirmNewPassword.setRequired(true);
		confirmNewPassword.setRequiredError("Field is required");
		
		addField("currentPassword", currentPassword);
		addField("newPassword", newPassword);
		addField("confirmNewPassword", confirmNewPassword);
		
		final Button save = new Button("Save");
		save.addStyleName("blueButton");
		save.addListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
            	
            	if(isValid()){
            		String currentPass = User.hash((currentPassword.getValue().toString()));
                	String newPass = newPassword.getValue().toString();
                	String confNewPass = confirmNewPassword.getValue().toString();
                	EntityManager em = app.em;
                	User user = em.find(User.class, app.getUser().getUserId());
                	Boolean currentPassCorrect = currentPass.equalsIgnoreCase(user.getPassword());
                	Boolean newAndConfPassEqual = newPass.equalsIgnoreCase(confNewPass);
                	if(currentPassCorrect && newAndConfPassEqual){
	                	try{
		        			em.getTransaction().begin();
		        			user.setPassword(newPass);
		        			em.merge(user);
		        			em.getTransaction().commit();
		        			save.setEnabled(false);
		        			app.showNotification(Constants.SUCCESSFULLY_SAVED_MESSAGE);
		        		} catch (Exception e) {
		        			app.showNotification("Oooopss something whent wrong !!..");
		        			em.getTransaction().rollback();
		        		}
                	} else if(!currentPassCorrect){
                		app.showNotification("Current password is incorrect");
                	} else if(!newAndConfPassEqual){
                		app.showNotification("Please ensure that new and confirmed password are the same");
                	}
            	} else{
            		app.showNotification("Invalid Input: Please ensure all required fields contain valid data before saving.");
            	}
            }
        });
		
		getFooter().addComponent(save);
		getLayout().setMargin(true);
		getFooter().setMargin(false, true, false, true);
		((HorizontalLayout) getFooter()).setSpacing(true);
	}
}
